package com.xinqi.modules.chat.chat.group.controller.app;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.green.annotation.Text;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.chat.chat.group.dto.req.ChatClassUserGroupCreateDTO;
import com.xinqi.modules.chat.chat.group.dto.req.ChatGroupQueryDTO;
import com.xinqi.modules.chat.chat.group.dto.req.ChatGroupUpdateDTO;
import com.xinqi.modules.chat.chat.group.dto.req.ChatUserGroupCreateDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.ChatGroupResultDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.ChatGroupUserResultDTO;
import com.xinqi.modules.chat.chat.group.service.ChatGroupService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * APP -- 群组
 *
 * @version V1.0
 * @Title:
 * @ClassName: com.xinqi.modules.chat.chat.group.controller.app.ChatGroupAppController.java
 * @Description:
 * @Copyright 星齐科技（云南）有限公司 - Powered By 技术部
 * @author: yyzhen
 * @date: 2023/5/19 10:07
 */
@Validated
@RestController
@RequestMapping("/app/chat-group")
@RequiredArgsConstructor
public class ChatGroupAppController {
    private final ChatGroupService chatGroupService;

    @Operation(summary = "我的群列表")
    @PostMapping("/list")
    public R<List<ChatGroupResultDTO>> list(@Text @RequestParam(value = "keywords", required = false) String keywords) {
        return R.data(chatGroupService.listByUserId(LoginHelper.getUserId(), keywords));
    }

    @Operation(summary = "我的群列表 -- 分页")
    @GetMapping("/page")
    public R<PageInfo<ChatGroupResultDTO>> page(Param pageable, @Validated ChatGroupQueryDTO query) {
        query.setOperationUserId(LoginHelper.getUserId());
        return R.ok(chatGroupService.pageByQuery(pageable, query));
    }

    @Operation(summary = "创建群组-当前登录用户")
    @PostMapping("/user-create")
    public R<ChatGroupResultDTO> userCreate(@Validated @RequestBody ChatUserGroupCreateDTO dto) {
        ChatGroupResultDTO groupResultDTO = chatGroupService.userCreate(LoginHelper.getXqLoginUser(), dto);
        return R.data(groupResultDTO, MessageUtils.message("chat.group.creat.success"));
    }

    @Operation(summary = "创建群组-班级群")
    @PostMapping("/class-create")
    public R<ChatGroupResultDTO> classCreate(@Validated @RequestBody ChatClassUserGroupCreateDTO dto) {
        ChatGroupResultDTO groupResultDTO = chatGroupService.classCreate(LoginHelper.getXqLoginUser(), dto);
        return R.data(groupResultDTO, MessageUtils.message("chat.group.creat.success"));
    }

    @Operation(summary = "设置群-群主-管理")
    @PutMapping("/set-group/{id}")
    public R<Boolean> setChatGroup(@NotNull(message = "{chat.group.id.not.blank}") @PathVariable("id") Long id,
                                   @Validated @RequestBody ChatGroupUpdateDTO dto) {
        dto.setId(id);
        Boolean result = chatGroupService.setChatGroup(LoginHelper.getXqLoginUser(), dto);
        return R.data(result);
    }

    @Operation(summary = "解散群-群主")
    @PutMapping("/disband-group/{id}")
    public R<Boolean> disbandChatGroup(@NotNull(message = "{chat.group.id.not.blank}") @PathVariable("id") Long id) {
        Boolean result = chatGroupService.disbandChatGroup(LoginHelper.getXqLoginUser(), id);
        return R.data(result);
    }

    @Operation(summary = "转让群-群主")
    @PutMapping("/shift-group/{id}")
    public R<Boolean> shiftChatGroup(@NotNull(message = "{chat.group.id.not.blank}") @PathVariable("id") Long id
        , @Validated @RequestBody ChatGroupUpdateDTO dto) {
        dto.setId(id);
        Boolean result = chatGroupService.shiftChatGroup(LoginHelper.getXqLoginUser(), dto);
        return R.data(result);
    }

    @Operation(summary = "根据班级查询班级群")
    @GetMapping("class-group-info/{classId}")
    public R<ChatGroupUserResultDTO> groupInfoByClass(@NotNull(message = "{chat.group.classId.not.blank}") @PathVariable("classId") Long classId) {
        return chatGroupService.groupInfoByClass(LoginHelper.getUserId(), classId);
    }

    @Operation(summary = "群组-详情")
    @GetMapping("/{groupId}")
    public R<ChatGroupResultDTO> selectInfoByGroupId(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @PathVariable("groupId") String groupId) {
        return R.data(chatGroupService.findByGroupIdIncludingUser(groupId, LoginHelper.getUserId()));
    }

}
